/**
* Created with IntelliJ IDEA.
*/
var ${bean.lowerClassName}_save_button = '.${bean.lowerClassName}_save_button';
var ${bean.lowerClassName}_reset_button = '.${bean.lowerClassName}_reset_button';
var ${bean.lowerClassName}_add_button = '${bean.lowerClassName}_add_button';
var ${bean.lowerClassName}_modify_button = '${bean.lowerClassName}_modify_button';
var ${bean.lowerClassName}_view_button = '${bean.lowerClassName}_view_button';
var ${bean.lowerClassName}_delete_button = '${bean.lowerClassName}_delete_button';

var ${bean.lowerClassName}_add_window = '${bean.lowerClassName}_add_window';
var ${bean.lowerClassName}_modify_window = '${bean.lowerClassName}_modify_window';
var ${bean.lowerClassName}_view_window = '${bean.lowerClassName}_view_window';

var ${bean.lowerClassName}_table = '${bean.lowerClassName}_table';

var ${bean.lowerClassName}_url_add = Constants.ctx + "${bean.lowerClassName}/add.do";
var ${bean.lowerClassName}_url_edit = Constants.ctx + "${bean.lowerClassName}/edit.do";
var ${bean.lowerClassName}_url_view = Constants.ctx + "${bean.lowerClassName}/view.do";
var ${bean.lowerClassName}_url_deleteMore = Constants.ctx + "${bean.lowerClassName}/deleteMore.do";
var ${bean.lowerClassName}_url_save = Constants.ctx + "${bean.lowerClassName}/save.do";

//添加
$("#" + ${bean.lowerClassName}_add_button).off().on("click", function (e) {
$("#" + ${bean.lowerClassName}_add_window).load(${bean.lowerClassName}_url_add, function () {
Utils.openWindow(${bean.lowerClassName}_add_window);
var formObj = $(this).find('form').eq(0);
formObj.form('clear');
validation(formObj);
registeEvent(${bean.lowerClassName}_add_window);
})
e.stopPropagation();
});

//修改
$("#" + ${bean.lowerClassName}_modify_button).off().on("click", function (e) {
function callBack(row) {
var urlParams = '?id=' + row.id;
$("#" + ${bean.lowerClassName}_modify_window).load(${bean.lowerClassName}_url_edit + urlParams, function () {
Utils.openWindow(${bean.lowerClassName}_modify_window);
initComplexObject(row);
var formObj = $(this).find('form').eq(0);
formObj.form('load', row);
validation(formObj);
registeEvent(${bean.lowerClassName}_modify_window);
})
}
Utils.doEdit(callBack, {elementId: ${bean.lowerClassName}_table});
e.stopPropagation();
});

//查看
$("#" + ${bean.lowerClassName}_view_button).off().on("click", function (e) {
function callBack(row) {
var urlParams = '?id=' + row.id;
$("#" + ${bean.lowerClassName}_view_window).load(${bean.lowerClassName}_url_view + urlParams, function () {
Utils.openWindow(${bean.lowerClassName}_view_window);
initComplexObject(row);
var formObj = $(this).find('form').eq(0);
formObj.form('load', row);
formObj.find('input').each(function () {
$(this).attr('disabled', 'disabled');
$(this).attr('readonly', true);
})
registeEvent(${bean.lowerClassName}_view_window);
})
}
Utils.doView(callBack, {elementId: ${bean.lowerClassName}_table});
e.stopPropagation();
});

//删除
$("#" + ${bean.lowerClassName}_delete_button).off().on("click", function (e) {
function callBack(flag) {
}

Utils.doDeleteMore(callBack, {elementId: ${bean.lowerClassName}_table, url: ${bean.lowerClassName}_url_deleteMore});
e.stopPropagation();
});


function validation(formObj) {
var conditions = {
//        id: {},
<#list bean['fields'] as field>
    <#if field.columnConditionUI != "">
    ${field.columnConditionUI},
    </#if>
</#list>
description: {},
createTime: {element: 'datetimebox', formatter: Utils.formaterDateTime},
createuser: {},
updateTime: {element: 'datetimebox', formatter: Utils.formaterDateTime},
updateuser: {}
};
Utils.initFormConditions(formObj, conditions);
}

function registeEvent(windowId) {
var formObj = $('#' + windowId).find('form').eq(0);
$('#' + windowId).find(${bean.lowerClassName}_save_button).eq(0).off().on("click", function (e) {
var isValid = formObj.form('validate');
if (!isValid)
return;
$(this).attr('disabled', 'disabled');
var obj = {};
obj.formObj = formObj;
obj.url = ${bean.lowerClassName}_url_save;
function dealAjaxBack(data) {
Utils.closeWindow(windowId);
$.messager.alert('Info', data, 'info');
Utils.refreshGrid(${bean.lowerClassName}_table);
}

obj.dealAjaxBack = dealAjaxBack;
Utils.submitForm(obj);
e.stopPropagation();
});

$('#' + windowId).find(${bean.lowerClassName}_reset_button).eq(0).off().on("click", function (e) {
Utils.resetForm(formObj);
e.stopPropagation();
});
}

function initComplexObject(data) {
<#list bean['fields'] as field>
    <#if field.relationObject!="">
    data['${field.relationObject}.id'] = data.${field.relationObject}.id;
    data['${field.relationObject}.name'] = data.${field.relationObject}.name;
    </#if>
</#list>
}
